/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io;

import org.openaecfm.spf.model.HeaderEntity;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.StringParameter;
import org.openaecfm.spf.model.FileName;

/**
 * Input and output FileName.
 * @author Qiu Kuining
 */
public final class FileNameIO {

    /**
     * Export FileName.
     * @param pojo
     * @return
     */
    public HeaderEntity exportSPF(FileName pojo) {
        HeaderEntity he = new HeaderEntity("FILE_NAME");

        // Set Name
        he.addParameter(new StringParameter(pojo.getName(), StringParameter.UTF_8));

        // Set Time_stamp
        he.addParameter(new StringParameter(pojo.getTime_stamp(), StringParameter.UTF_8));

        // Set Author
        ListParameter author = new ListParameter();
        for (String s : pojo.getAuthor()) {
            author.add(new StringParameter(s, StringParameter.UTF_8));
        }
        he.addParameter(author);

        // Set Organization
        ListParameter organization = new ListParameter();
        for (String s : pojo.getOrganization()) {
            organization.add(new StringParameter(s, StringParameter.UTF_8));
        }
        he.addParameter(organization);

        // Set Preprocessor_version
        he.addParameter(new StringParameter(pojo.getPreprocessor_version(), StringParameter.UTF_8));

        // Set Originating_system
        he.addParameter(new StringParameter(pojo.getOriginating_system(), StringParameter.UTF_8));

        // Set Authorization
        he.addParameter(new StringParameter(pojo.getAuthorization(), StringParameter.UTF_8));

        return he;
    }

    /**
     * Import FileName.
     * @param he
     * @return
     */
    public FileName importSPF(HeaderEntity he) {
        FileName pojo = new FileName();

        // Set Name
        StringParameter name = (StringParameter) he.getParameter(0);
        pojo.setName(name.toUTF8());

        // Set Time_stamp
        StringParameter time_stamp = (StringParameter) he.getParameter(1);
        pojo.setTime_stamp(time_stamp.toUTF8());

        // Set Author
        ListParameter author = (ListParameter) he.getParameter(2);
        for (Parameter p : author) {
            StringParameter s = (StringParameter) p;
            pojo.addAuthor(s.toUTF8());
        }

        // Set Organization
        ListParameter organization = (ListParameter) he.getParameter(3);
        for (Parameter p : organization) {
            StringParameter s = (StringParameter) p;
            pojo.addOrganization(s.toUTF8());
        }

        // Set Preprocessor_version
        StringParameter preprocessor_version = (StringParameter) he.getParameter(4);
        pojo.setPreprocessor_version(preprocessor_version.toUTF8());

        // Set Originating_system
        StringParameter originating_system = (StringParameter) he.getParameter(5);
        pojo.setOriginating_system(originating_system.toUTF8());

        // Set Authorization
        StringParameter authorization = (StringParameter) he.getParameter(6);
        pojo.setAuthorization(authorization.toUTF8());

        return pojo;
    }
}
